class Solution
{
public:
    bool equalFrequency(string word)
    {
        unordered_map<char, int> charCount;
        for (char c : word)
        {
            ++charCount[c];
        }
        map<int, int> countNum;
        for (auto [c, count] : charCount)
        {
            ++countNum[count];
        }
        if (countNum.size() == 1)
        {
            return countNum.begin()->first == 1 || countNum.begin()->second == 1;
        }
        if (countNum.size() != 2)
        {
            return false;
        }
        int lowerCount = countNum.begin()->first;
        if (lowerCount == 1 && countNum[lowerCount] == 1)
        {
            return true;
        }
        if (!countNum.count(lowerCount + 1))
        {
            return false;
        }
        if (countNum[lowerCount + 1] != 1)
        {
            return false;
        }
        return true;
    }
};